﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.SECURITY.App_UI.UserControls;
using KPIS.GERP.SECURITY.DAL;

namespace KPIS.GERP.SECURITY
{
    public partial class ApprovePath : System.Web.UI.Page
    {
        ApprovePathDAL app = new ApprovePathDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (TableStep.Rows[0].Cells.Count == 0)
            {
                ddlProcess_SelectedIndexChanged(null, null);
            }
        }

        public void LoadStep()
        {
            DataTable dtb = app.GetFull(ddlProcess.SelectedValue);
            int nCount = dtb.Rows.Count;

            if (nCount > 0)
            {
                for (int i = 0; i < nCount; i++)
                {
                    Approver item = AddNewStep(i);
                    item.LoadData(dtb.Rows[i]);
                    item.SetControl(false);
                }

                Approver itemNew = AddNewStep(nCount);
                itemNew.SetControl(true);
            }
            else
            {
                Approver itemNew = AddNewStep(0);
                itemNew.SetControl(true);
            }
        }

        public Approver AddNewStep(int i)
        {
            if (i > 0)
            {
                System.Web.UI.HtmlControls.HtmlTableCell cellArrow = new System.Web.UI.HtmlControls.HtmlTableCell();
                Label lbl = new Label();
                lbl.Text = " >> ";
                lbl.CssClass = "Arrow";
                cellArrow.Controls.Add(lbl);
                TableStep.Rows[0].Cells.Add(cellArrow);
            }

            System.Web.UI.HtmlControls.HtmlTableCell cell = new System.Web.UI.HtmlControls.HtmlTableCell();
            Approver item = (Approver)Page.LoadControl("~/App_UI/UserControls/Approver.ascx");
            item.ID = "Approver" + i.ToString();
            item.SetStepNo(i + 1);
            cell.ID = "cell" + i.ToString();
            cell.Controls.Add(item);
            TableStep.Rows[0].Cells.Add(cell);

            return item;
        }

        protected void ddlProcess_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["Process"] = ddlProcess.SelectedValue;
            LoadStep();
        }
    }
}
